﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaytracerLib.MathLib;
using TomanuExtensions;

namespace RaytracerLib.UVMappers
{
    public class SphericalUVMapper : UVMapper
    {
        public override Vector2 Map(Vector3 a_uvw)
        {
            double v = Math.Acos(DoubleExtensions.Limit(a_uvw.Y * 2 - 1, -1, 1)) / MathExtensions.PI;
            double u = Math.Atan2(a_uvw.Z - 0.5, a_uvw.X - 0.5) / (2 * MathExtensions.PI);

            if (a_uvw.Z <= 0.5)
                u = u + 1;

            return new Vector2(u, v);
        }

        public override UVMappingMethod Method
        {
            get
            {
                return UVMappingMethod.Spherical;
            }
        }
    }
}
